package com.java.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author liyanhao
 * @Date 2023-02-25 13:42
 */
public class Tank3 implements Moveable {

    public static void main(String[] args) {
        Tank3 tank = new Tank3();
        Moveable o = (Moveable)Proxy.newProxyInstance(Tank3.class.getClassLoader(),
                new Class[]{Moveable.class}, new Tank3Proxy(tank));
        o.move();

    }

    @Override
    public void move() {
        System.out.println("Tank3 打印");
    }
}

class Tank3Proxy implements InvocationHandler {
    private Moveable moveable;

    public Tank3Proxy(Moveable moveable) {
        this.moveable = moveable;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("添加代理类日志");
        Object invoke = method.invoke(moveable, args);
        System.out.println("添加代理类日志结束");
        return invoke;
    }
}
interface Moveable{
    void move();
}